/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.service.billnegotiated;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.financial.dao.billnegotiated.BillNegotiatedDao;
import com.jeesite.modules.financial.dao.billnegotiated.BillNegotiatedDetailDao;
import com.jeesite.modules.financial.entity.billnegotiated.BillNegotiated;
import com.jeesite.modules.financial.entity.billnegotiated.BillNegotiatedDetail;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 到单押汇Service
 * @author Kilo
 * @version 2021-03-10
 */
@Service
@Transactional(readOnly=true)
public class BillNegotiatedService extends CrudService<BillNegotiatedDao, BillNegotiated> {
	
	@Autowired
	private BillNegotiatedDetailDao billNegotiatedDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param billNegotiated
	 * @return
	 */
	@Override
	public BillNegotiated get(BillNegotiated billNegotiated) {
		BillNegotiated entity = super.get(billNegotiated);
		if (entity != null){
			BillNegotiatedDetail billNegotiatedDetail = new BillNegotiatedDetail(entity);
			billNegotiatedDetail.setStatus(BillNegotiatedDetail.STATUS_NORMAL);
			entity.setBillNegotiatedDetailList(billNegotiatedDetailDao.findList(billNegotiatedDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param billNegotiated 查询条件
	 * @param billNegotiated.page 分页对象
	 * @return
	 */
	@Override
	public Page<BillNegotiated> findPage(BillNegotiated billNegotiated) {
		return super.findPage(billNegotiated);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param billNegotiated
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(BillNegotiated billNegotiated) {
		if(billNegotiated.getIsNewRecord()){
			billNegotiated.setBillNegotiatedCode(codeRuleService.getCode("BillNegotiated"));
			billNegotiated.setCreateBy(UserUtils.getUser().getLoginCode());
		}
		super.save(billNegotiated);
		if (billNegotiated.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("BillNegotiated");
		}
		// 保存上传附件
		FileUploadUtils.saveFileUpload(billNegotiated.getId(), "billNegotiated_file");
		// 保存 BillNegotiated子表
		for (BillNegotiatedDetail billNegotiatedDetail : billNegotiated.getBillNegotiatedDetailList()){
			if (!BillNegotiatedDetail.STATUS_DELETE.equals(billNegotiatedDetail.getStatus())){
				billNegotiatedDetail.setBillNegotiatedId(billNegotiated);
				if (billNegotiatedDetail.getIsNewRecord()){
					billNegotiatedDetailDao.insert(billNegotiatedDetail);
				}else{
					billNegotiatedDetailDao.update(billNegotiatedDetail);
				}
			}else{
				billNegotiatedDetailDao.delete(billNegotiatedDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param billNegotiated
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(BillNegotiated billNegotiated) {
		super.updateStatus(billNegotiated);
	}
	
	/**
	 * 删除数据
	 * @param billNegotiated
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(BillNegotiated billNegotiated) {
		super.delete(billNegotiated);
		BillNegotiatedDetail billNegotiatedDetail = new BillNegotiatedDetail();
		billNegotiatedDetail.setBillNegotiatedId(billNegotiated);
		billNegotiatedDetailDao.deleteByEntity(billNegotiatedDetail);
	}
	
}